﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Threading;

namespace WorkflowLibrary
{

    public partial class ShowForm : System.Workflow.ComponentModel.Activity
    {
        #region events and event handlers

        public delegate void FormShownEventHandler(object sender,
            FormShownEventArgs e);
        private event FormShownEventHandler formShownEvent;
        public event FormShownEventHandler FormShown
        {
            add
            {
                formShownEvent += value;
            }
            remove
            {
                formShownEvent -= value;
            }
        }

        #endregion

        #region Execution

        protected override ActivityExecutionStatus Execute
            (ActivityExecutionContext context)
        {
            //Thread.Sleep(5000);
            if (formShownEvent != null)
            {
                formShownEvent(null, new FormShownEventArgs(FormGuid,WorkflowInstanceId,StructureId));
            }
            // Notifiy the runtime that the activity has finished
            return ActivityExecutionStatus.Closed;
        }

        #endregion

        #region Dependency properties

        public static DependencyProperty FormGuidProperty =
                    DependencyProperty.Register("FormGuid",
                    typeof(System.Guid),
                    typeof(ShowForm));

        [DesignerSerializationVisibilityAttribute
            (DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [DescriptionAttribute("Guid of form to be shown")]
        [CategoryAttribute("ShowForm Property")]
        public System.Guid FormGuid
        {
            get
            {
                return ((System.Guid)(base.GetValue(ShowForm.FormGuidProperty)));
            }
            set
            {
                base.SetValue(ShowForm.FormGuidProperty, value);
            }
        }

        public static DependencyProperty WorkflowInstanceIdProperty =
                    DependencyProperty.Register("WorkflowInstanceId",
                    typeof(System.Guid),
                    typeof(ShowForm));

        [DesignerSerializationVisibilityAttribute
            (DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [DescriptionAttribute("Guid of activity's workflow")]
        [CategoryAttribute("ShowForm Property")]
        public new System.Guid WorkflowInstanceId
        {
            get
            {
                return ((System.Guid)(base.GetValue(ShowForm.WorkflowInstanceIdProperty)));
            }
            set
            {
                base.SetValue(ShowForm.WorkflowInstanceIdProperty, value);
            }
        }

        public static DependencyProperty StructureIdProperty =
                    DependencyProperty.Register("StructureId",
                    typeof(System.Guid),
                    typeof(ShowForm));

        [DesignerSerializationVisibilityAttribute
            (DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [DescriptionAttribute("Guid of activity's workflow")]
        [CategoryAttribute("ShowForm Property")]
        public System.Guid StructureId
        {
            get
            {
                return ((System.Guid)(base.GetValue(ShowForm.StructureIdProperty)));
            }
            set
            {
                base.SetValue(ShowForm.StructureIdProperty, value);
            }
        }

        #endregion 

        #region Construction

        public ShowForm()
		{
			InitializeComponent();
        }

        #endregion

    }


    public class FormShownEventArgs
    {
        private Guid formGuid;
        private Guid workflowInstanceId;
        private Guid structureId;


        public FormShownEventArgs(Guid _fGuid, Guid _wGuid, Guid _sGuid)
        {
            this.formGuid = _fGuid;
            this.workflowInstanceId = _wGuid;
            this.structureId = _sGuid;
        }

        public Guid FormGuid
        {
            get { return this.formGuid; }
        }

        public Guid WorkflowInstanceId
        {
            get { return this.workflowInstanceId; }
        }

        public Guid StructureId
        {
            get { return this.structureId; }
        }

    }

}
